using System;
using System.Collections.Generic;
using System.Text;

namespace Skywave.IO
{
    public static class FileHelper
    {
        static object lock_GetFreeFileNameId = new object();
        public static int GetFreeFileNameId(string preFileName, string extension, string searchDirectory, int startIndex)
        {
            int r = -1;
            lock (lock_GetFreeFileNameId)
            {
                if (!searchDirectory.EndsWith("\\")) searchDirectory += "\\";
                int i = startIndex;
                while (System.IO.File.Exists(searchDirectory + preFileName + i.ToString() + extension)) i++;
                r = i;
            }
            return r;
        }

        static object lock_GetFreeFilePath = new object();
        public static string GetFreeFilePath(string preFileName, string extension, string searchDirectory, int startIndex, bool createReservedEmptyFile)
        {
            string r = "";
            lock (lock_GetFreeFilePath)
            {
                if (!searchDirectory.EndsWith("\\")) searchDirectory += "\\";
                int i = startIndex;
                while (System.IO.File.Exists(searchDirectory + preFileName + i.ToString() + extension)) i++;
                r = searchDirectory + preFileName + i.ToString() + extension;
                if (createReservedEmptyFile)
                {
                    try
                    {
                        System.IO.File.Create(r).Close();
                    }
                    catch (Exception)
                    {
                        r = "";
                    }
                }
            }
            return r;
        }

        public static List<byte[]> TransferGetBlocks(string filePath, int blockSizeInKB)
        {
            List<byte[]> r = null;
            if (!System.IO.File.Exists(filePath)) return null;
            using (System.IO.FileStream fileStream1 = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                r = TransferGetBlocks(fileStream1, blockSizeInKB);
            }
            return r;
        }
        public static List<byte[]> TransferGetBlocks(System.IO.Stream stream, int blockSizeInKB)
        {
            List<byte[]> r = new List<byte[]>();
            byte[] temp1;
            System.IO.BinaryReader reader1 = new System.IO.BinaryReader(stream);

            while (reader1.BaseStream.Position < reader1.BaseStream.Length)
            {
                temp1 = reader1.ReadBytes(blockSizeInKB * 1024);
                r.Add(temp1);
            }
            reader1.Close();
            return r;
        }
        public static void TransferCreateFileFromBlocks(List<byte[]> blocks, string filePath)
        {
            System.IO.FileStream fileStream1 = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
            System.IO.BinaryWriter writer1 = new System.IO.BinaryWriter(fileStream1);
            for (int i = 0; i < blocks.Count; i++)
            {
                writer1.Write(blocks[i]);
            }
            writer1.Close();
        }
    }
}
